package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.StudentOfficial;
import cn.wolfcode.crm.query.ChartQueryObject;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.query.StudentOfficialQueryObject;
import cn.wolfcode.crm.service.IChartService;
import cn.wolfcode.crm.service.IStudentOfficialService;
import cn.wolfcode.crm.util.PageResult;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Controller
@RequestMapping("chart")
public class ChartController {
    @Autowired
    private IChartService chartService;

    //正式学员统计表
    @RequestMapping("view")
    public String stuReportView(){

        return "chart/chart";
    }

    @RequestMapping("report")
    @ResponseBody
    public Object getStuReport(ChartQueryObject qo){

        return chartService.selectStuReport(qo);
    }

    @RequestMapping("exportXls")
    public void exportXls(HttpServletResponse response, ChartQueryObject qo) throws Exception {
        //设置文件下载响应头
        response.setHeader("Content-Disposition", "attachment;filename=chart.xls");

        Workbook book = chartService.exportXls(qo);

        //写入数据输出到浏览器
        book.write(response.getOutputStream());
    }


    @RequestMapping("chartBybar")
    public String saleChartByBar(ChartQueryObject qo, Model model){
        List<Map<String, Object>> maps = chartService.selectStuReport(qo);

        //分组类型如：["衬衫","羊毛衫","雪纺衫","裤子","高跟鞋","袜子"]
        List<String> groupByType = new ArrayList<>();

        //存放分组数据
        List<String> groupByValue = new ArrayList<>();

        for (Map<String, Object> map : maps) {
            groupByValue.add(map.get("waitingFit").toString());
            groupByValue.add(map.get("payFit").toString());
            groupByValue.add(map.get("payState").toString());
            groupByType.add(map.get("groupByTypeName").toString());
            groupByType.add(map.get("groupByTypeName").toString());
            groupByType.add(map.get("groupByTypeName").toString());


        }
        String datas = "";
        String values = "";
        try {
            ObjectMapper mapper = new ObjectMapper();
            datas = mapper.writeValueAsString(groupByType);
            values = mapper.writeValueAsString(groupByValue);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }

        model.addAttribute("datas",datas);
        model.addAttribute("values",values);

        return "chart/chartByBar";
    }

    @RequestMapping("chartByPie")
    public String saleByPie(ChartQueryObject qo, Model model){
        List<Map<String, Object>> maps = chartService.selectStuReport(qo);
        //分组类型如：["衬衫","羊毛衫","雪纺衫","裤子","高跟鞋","袜子"]

        List<String> groupByType = new ArrayList<>();
        List<Map<String,Object>> groupByValues = new ArrayList<>();

        for (Map<String, Object> map : maps) {
            groupByType.add(map.get("groupByTypeName").toString());

            Map<String,Object> groupByValue = new HashMap<>();
            groupByValue.put("value",map.get("payState"));
            groupByValue.put("name",map.get("groupByTypeName"));
            groupByValues.add(groupByValue);
        }

        //存放分组数据
        String datas = "";
        String values = "";
        try {
            ObjectMapper mapper = new ObjectMapper();
            datas = mapper.writeValueAsString(groupByType);
            values = mapper.writeValueAsString(groupByValues);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        model.addAttribute("datas",datas);
        model.addAttribute("values",values);
        model.addAttribute("titelName",qo.getGroupByType());

        return "chart/chartByPie";
    }
}
